{% extends "base-new.html" %}
{% block header-content %}
<h2>ROTModel</h2>
<div>
    <p>
        ROTModel overrides the db.Model functions, retrying each method each time
    a timeout exception is raised.
    </p>

    <p>
    Methods superclassed from db.Model are:
        <ul>
            <li>get(cls, keys)</li>
            <li>get(cls, keys)</li>
            <li>get_by_id(cls, ids, parent)</li>
            <li>get_by_key_name(cls, key_names, parent)</li>
            <li>get_or_insert(cls, key_name, kwargs)</li>
            <li>put(self)</li>
        </ul>
    </p>
</div>
{% endblock %}
{% block content %}
<table>
    <tr>
        <th colspan="2">
            Unit Tests
        </th>
    </tr>
    <tr>
        <td>modelcreate</td><td>{{ modelcreate }}</td>
    </tr>
    <tr>
        <td>puttest</td><td>{{ puttest }}</td>
    </tr>
    <tr>
        <td>savedtest1</td><td>{{ savedtest1 }}</td>
    </tr>
    <tr>
        <td>savedtest2</td><td>{{ savedtest2 }}</td>
    </tr>
    <tr>
        <td>get_or_insert</td><td>{{ get_or_insert }}</td>
    </tr>
    <tr>
        <td>singleget</td><td>{{ singleget }}</td>
    </tr>
    <tr>
        <td>multiget</td><td>{{ multiget }}</td>
    </tr>
    <tr>
        <td>keytest</td><td>{{ keytest }}</td>
    </tr>
    <tr>
        <td>getbykeynamesingle</td><td>{{ getbykeynamesingle }}</td>
    </tr>
    <tr>
        <td>getbykeynamemulti</td><td>{{ getbykeynamemulti }}</td>
    </tr>
    <tr>
        <td>alltest</td><td>{{ alltest }}</td>
    </tr>
    <tr>
        <td>gqltest</td><td>{{ gqltest }}</td>
    </tr>
    <tr>
        <td>parenttest</td><td>{{ parenttest }}</td>
    </tr>
    <tr>
        <td>parentkeytest</td><td>{{ parentkeytest }}</td>
    </tr>
    <tr>
        <td>deletetest</td><td>{{ deletetest }}</td>
    </tr>
</table>
<div class="code">
<h2>webapp class</h2>
<pre class="sh_python">{% filter escape %}class TestROTModel(ROTModel):
    testval = db.IntegerProperty()

class ROTModelPage(webapp.RequestHandler):
  def get(self):
      template_values = {}

      # create a model test
      model1 = TestROTModel(key_name="testmodel1", testval=1)
      if model1:
          template_values["modelcreate"] = "OK"
      else:
          template_values["modelcreate"] = "ERROR"

      # is_saved test 1
      if model1.is_saved() is False:
          template_values["savedtest1"] = "OK"
      else:
          template_values["savedtest1"] = "ERROR"

      # model put test
      model_key = model1.put()
      if model_key:
          template_values["puttest"] = "OK"
      else:
          template_values["puttest"] = "ERROR"

      # is_saved test 2
      if model1.is_saved() == True:
          template_values["savedtest2"] = "OK"
      else:
          template_values["savedtest2"] = "ERROR"

      # get test single
      singletest = TestROTModel.get(model_key)
      if singletest:
          template_values["singleget"] = "OK"
      else:
          template_values["singleget"] = "ERROR"

      # get_or_insert test
      model2 = TestROTModel.get_or_insert("testmodel2", parent=model1, testval=2)
      if model2:
          template_values["get_or_insert"] = "OK"
          model2_key = model2.put()
      else:
          template_values["get_or_insert"] = "ERROR"

      # get test multi
      multitest = TestROTModel.get([model_key, model2_key])
      if len(multitest) > 1:
          template_values["multiget"] = "OK"
      else:
          template_values["multiget"] = "ERROR"

      # key test
      model2_key = model2.key()
      if model2_key:
          template_values["keytest"] = "OK"
      else:
          template_values["keytest"] = "ERROR"

      # set strings for key names
      model_keyname = "testmodel1"
      model2_keyname = "testmodel2"

      # get_by_key_name single
      singlekeyname = TestROTModel.get_by_key_name(model_keyname)
      if singlekeyname:
          template_values["getbykeynamesingle"] = "OK"
      else:
          template_values["getbykeynamesingle"] = "ERROR"

      # get_by_key_name multi
      multikeyname = TestROTModel.get_by_key_name([model_keyname, model2_keyname])
      if multikeyname:
          template_values["getbykeynamemulti"] = "OK"
      else:
          template_values["getbykeynamemulti"] = "ERROR"

      # all test
      alltest = TestROTModel.all()
      results = alltest.fetch(20)
      if len(results) > 0:
          template_values["alltest"] = "OK"
      else:
          template_values["alltest"] = "ERROR"

      # gql test
      gqltest = TestROTModel.gql("WHERE testval = :1", 1)
      results = gqltest.fetch(20)
      if len(results) > 0:
          template_values["gqltest"] = "OK"
      else:
          template_values["gqltest"] = "ERROR"

      # parent test
      parenttest = model2.parent()
      if parenttest:
          template_values["parenttest"] = "OK"
      else:
          template_values["parenttest"] = "ERROR"

      # parent_key test
      parentkeytest = model2.parent_key()
      if parentkeytest == model_key:
          template_values["parentkeytest"] = "OK"
      else:
          template_values["parentkeytest"] = "ERROR"

      # delete test
      model1.delete()
      model2.delete()

      if TestROTModel.get(model_key) == None:
          template_values["deletetest"] = "OK"
      else:
          template_values["deletetest"] = "ERROR"


      path = os.path.join(os.path.dirname(__file__), 'templates/rotmodel.html')
      self.response.out.write(template.render(path, template_values))

{% endfilter %}</pre>
</div>
{% endblock %}
